using UnityEngine;
using System.Collections;
using System.IO;

/**
 * Script of file writing and reading.
 * The purpose of this class is to read and write a save file.
 * @author Benjamin Bruneau
 */
public class SaveManagerScript : MonoBehaviour 
{
	private const string GAME_MUSIC_NAME = "GameMusic"; // GameMusic GameObject's name
	private const string MENU_MUSIC_NAME = "MenuMusic"; // MenuMusic GameObject's name
	private const string SAVE_URL = "save.save"; // Path for save file
	private const string MUSIC_SAVE_URL = "musicsave.save"; // Path for music save file
	private const string SEPARATOR = ";"; // Separator character
	private const string NO_REWARD = "none"; // No Reward
	private const string LEVEL_PREFIX = "PrelevelScene"; // Level Prefix
	
	private const char SEPARATOR_CHAR = ';'; // Separator character in char
	
	private const int LEVEL_COUNT = 9; // Level count
	
	private const float DEFAULT_TIME = 100000f; // Default time
	
	private int _highestLevelUnlocked = 0; // Highest Level Unlocked

	public GameObject _playText; // GameObject to redirect
	
	public ArrayList _sequence; // Loaded file
	
	public bool _musicMute = true;
		
	/**
	 * Called on initialization.
	 * @return void
	 */
	void Start () {
	
		if (!File.Exists(SAVE_URL))
		{
			createNewSaveFile();
			loadSaveFile();
		} else {
			loadSaveFile();
		}
		
		if (!File.Exists(MUSIC_SAVE_URL))
		{
			createNewMusicSaveFile();
			loadMusicSaveFile();
		} else {
			loadMusicSaveFile();
		}
		
		// Set Play Text with saved datas
		_playText.GetComponent<DetectClickOnObjectScript>()._forward = LEVEL_PREFIX + _highestLevelUnlocked.ToString();
		
		//############################
		//_playText.GetComponent<DetectClickOnObjectScript>()._forward = "PrelevelScene1";
		//############################
		
		DontDestroyOnLoad(this);
	}
		
	/**
	 * Called once per frame.
	 * @return void
	 */
	void Update ()
	{	
		
		muteMusic();
		
	}
	
	/**
	 * Called to mute/unmute Music.
	 * @return void
	 */ 
	private void muteMusic()
	{
		//Mute / Unmute
		if (Input.GetKeyUp(KeyCode.M))
		{
			_musicMute = !_musicMute;
			
		StreamWriter streamWriter = new StreamWriter(MUSIC_SAVE_URL);
		
		streamWriter.WriteLine((_musicMute).ToString());
		
		streamWriter.Close();
			
		}
	}
	
	/**
	 * Flushes datas in file.
	 * @return void
	 */
	public void saveInFile() 
	{
		StreamWriter streamWriter = new StreamWriter(SAVE_URL);
		
		// Entire save
		foreach (ArrayList sublist in _sequence) 
		{
			string line = "";
			// Each line
			foreach (var subObject in sublist) 
			{
				if(!line.Equals(""))
				{
					line = line + SEPARATOR;
				}
				line = line + subObject.ToString();
			}
			streamWriter.WriteLine(line);
		}
		streamWriter.Close();
	}
	
	/**
	 * Creates new music save file.
	 * @return void
	 */
	private void createNewMusicSaveFile() 
	{
		StreamWriter streamWriter = new StreamWriter(MUSIC_SAVE_URL);
		
		streamWriter.WriteLine((false).ToString());
		
		streamWriter.Close();
	}
	
	/**
	 * Loads music datas.
	 * @return void
	 */
	private void loadMusicSaveFile()
	{
		
		// Open stream reader
 		StreamReader streamReader = new StreamReader(MUSIC_SAVE_URL); 
		
		// Read line
		string line = streamReader.ReadLine();
		
		_musicMute = bool.Parse(line);	
		// Close stream reader
		streamReader.Close(); 
	}
	
	/**
	 * Creates new save file.
	 * @return void
	 */
	private void createNewSaveFile() 
	{
		StreamWriter streamWriter = new StreamWriter(SAVE_URL);
		
		// Writes default datas for the new player
		for (int i = 0; i < 9; i++) {
			
			// Default : unlock the first level
			if(i == 0)
			{
				streamWriter.WriteLine((i+1) + SEPARATOR + true + SEPARATOR + DEFAULT_TIME + SEPARATOR + NO_REWARD);
				
			} else {
				
				streamWriter.WriteLine((i+1) + SEPARATOR + false + SEPARATOR + DEFAULT_TIME + SEPARATOR + NO_REWARD);
				
			}

		}
		streamWriter.Close();
	}
	
	/**
	 * Loads datas.
	 * @return void
	 */
	private void loadSaveFile()
	{
		_sequence = new ArrayList();
		
		// Open stream reader
 		StreamReader streamReader = new StreamReader(SAVE_URL); 
		
		// Read line
		string line = streamReader.ReadLine(); 
		while (line != null) 
		{ 
			// Split and format it
			string[] splitLine = line.Split(SEPARATOR_CHAR);
			_sequence.Add(new ArrayList(){int.Parse (splitLine[0]),bool.Parse(splitLine[1]), float.Parse(splitLine[2]), splitLine[3]});
			
			if(bool.Parse(splitLine[1]))
			{
				_highestLevelUnlocked++;
			}
			
			// Read line
			line = streamReader.ReadLine(); 
		} 
		
		// Close stream reader
		streamReader.Close(); 
	}
}
